setwd("~/Desktop/Datasets")
require(dplyr) 
require(plotrix)
require(lfe)
library(foreign)
library(knitr)
library(car)
library(sandwich)
library(lmtest)
library(plm)
library(Matching)
library(ebal)
library(sandwich)
library(lmtest)
library(lfe)
library(AER)
library(ivpack)
library(stargazer)
library(broom)
library(dotwhisker)
library(foreign)
library(MASS)
library(nnet)
library(dplyr)
library(boot)
library(sampling)

################################################ 
################################################ 
#### Cleaning Data  ############################ 
################################################ 
################################################

capital.data <- read.csv("capital.habeas.judges.readable.csv")
capital.data$Sentence.at.Appeal <- ifelse(capital.data$Sentence.at.Appeal=="Death", 1, 0)
capital.data$Conviction.original <- capital.data$Conviction
capital.data$Conviction <- ifelse((capital.data$Conviction==1|capital.data$Conviction==.5), 1, 0)
capital.data$LC.Conviction <- ifelse((capital.data$LC.Conviction==1|capital.data$LC.Conviction==.5), 1, 0)
capital.data$LC.Sentence <- ifelse((capital.data$LC.Sentence==1|capital.data$LC.Sentence==.5), 1, 0)
capital.data$Type.of.Crime.original <- capital.data$Type.of.Crime
capital.data$Type.of.Crime <- ifelse(capital.data$Type.of.Crime=="Murder 2"|capital.data$Type.of.Crime=="Felony Murder", 0, 1)
capital.data$D.s.Gender <- ifelse(capital.data$D.s.Gender=="M", 1, 0)
capital.data$Jurisidiction <- ifelse(capital.data$Jurisidiction=="Federal", 1, 0)
capital.data$Split.Opinion <- ifelse(capital.data$Split.Opinion=="Yes", 1, 0)
capital.data$race.original <- capital.data$D.s.Race

capital.data$Sentence <- ifelse(capital.data$Sentence=="1"|capital.data$Sentence==".5", 1, 0)
sum(ifelse(capital.data$Sentence.at.Appeal==1, capital.data$Conviction, 0), na.rm = T)

sum(ifelse(capital.data$Sentence.at.Appeal==0, capital.data$Conviction, 0), na.rm = T)

sum(ifelse(capital.data$Sentence.at.Appeal==1 & (capital.data$Conviction==0|capital.data$Conviction==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$Sentence.at.Appeal==0 & (capital.data$Conviction==0|capital.data$Conviction==1), 1, 0), na.rm = T)

###################################################################################
###################################################################################
###################################################################################################
#### Circuit Fixed Effects (From Earlier Coding and For MVN Simulation) ##########################################################################################################################################################################################################
#################################################################

capital.data$Federal.Circuit.Court.Number.1 <- ifelse(capital.data$Federal.Circuit.Court.Number==1, 1, 0)
capital.data$Federal.Circuit.Court.Number.2 <- ifelse(capital.data$Federal.Circuit.Court.Number==2, 1, 0)
capital.data$Federal.Circuit.Court.Number.3 <- ifelse(capital.data$Federal.Circuit.Court.Number==3, 1, 0)
capital.data$Federal.Circuit.Court.Number.4 <- ifelse(capital.data$Federal.Circuit.Court.Number==4, 1, 0)
capital.data$Federal.Circuit.Court.Number.5 <- ifelse(capital.data$Federal.Circuit.Court.Number==5, 1, 0)
capital.data$Federal.Circuit.Court.Number.6 <- ifelse(capital.data$Federal.Circuit.Court.Number==6, 1, 0)
capital.data$Federal.Circuit.Court.Number.7 <- ifelse(capital.data$Federal.Circuit.Court.Number==7, 1, 0)
capital.data$Federal.Circuit.Court.Number.8 <- ifelse(capital.data$Federal.Circuit.Court.Number==8, 1, 0)
capital.data$Federal.Circuit.Court.Number.9 <- ifelse(capital.data$Federal.Circuit.Court.Number==9, 1, 0)
capital.data$Federal.Circuit.Court.Number.10 <- ifelse(capital.data$Federal.Circuit.Court.Number==10, 1, 0)
capital.data$Federal.Circuit.Court.Number.11 <- ifelse(capital.data$Federal.Circuit.Court.Number==11, 1, 0)
capital.data$Federal.Circuit.Court.Number.D.C. <- ifelse(capital.data$Federal.Circuit.Court.Number=="D.C.", 1, 0)
capital.data$Federal.Circuit.Court.Number.SCOTUS <- ifelse(capital.data$Federal.Circuit.Court.Number=="SCOTUS", 1, 0)

####################################################################################
##################################################################################
#### Subsetting Cases for Analysis of Potential Trump Appointees ##################
##################################################################################
####################################################################################

Sykes.cases <- capital.data[capital.data$Judge=="Sykes",]
Tymkovich.cases <- capital.data[capital.data$Judge=="Tymkovich",]
Colloton.cases <- capital.data[capital.data$Judge=="Colloton",]
Hardiman.cases <- capital.data[capital.data$Judge=="Hardiman",]
Willet.cases <- capital.data[capital.data$Judge=="Willet",]
Kethledge.cases <- capital.data[capital.data$Judge=="Kethledge",]
Barrett.cases <- capital.data[capital.data$Judge=="Barrett",]
Gruender.cases <- capital.data[capital.data$Judge=="Gruender",]
Stras.cases <- capital.data[capital.data$Judge=="Stras",]
Eid.cases <- capital.data[capital.data$Judge=="Eid",]
Gorsuch.cases <- capital.data[capital.data$Judge=="Gorsuch",]
Grant.cases <- capital.data[capital.data$Judge=="Grant",]
Newsom.cases <- capital.data[capital.data$Judge=="Newsom",]
Pryor.cases <- capital.data[capital.data$Judge=="Pryor",]
Thapar.cases <- capital.data[capital.data$Judge=="Thapar",]
Larsen.cases <- capital.data[capital.data$Judge=="Larsen",]
Kavanaugh.cases <- capital.data[capital.data$Judge=="Kavanaugh",]

############################
############################
############################
#### More Cleaning Data  #### 
############################
############################
############################

capital.data <- capital.data[capital.data$D.s.Race!="Unknown",]

#### For testing more specific associations between race and outcome

capital.data$D.s.Race <- ifelse(capital.data$D.s.Race=="White", 1, 0)

### Coding Judge Party

capital.data$Party <- ifelse((capital.data$Judge=="Reinhardt"|capital.data$Judge=="Pregerson"|capital.data$Judge=="Nyguen"|capital.data$Judge=="Farris"|capital.data$Judge=="Owens"|capital.data$Judge=="Restrepo"|capital.data$Judge=="Fisher"|capital.data$Judge=="Phillips"|capital.data$Judge=="McHugh"|capital.data$Judge=="A. Jordan"|capital.data$Judge=="R. Anderson"|capital.data$Judge=="Hamilton"|capital.data$Judge=="Wood"|capital.data$Judge=="A. Williams"|capital.data$Judge=="Wardlaw"|capital.data$Judge=="Daughtrey"|capital.data$Judge=="Donald"|capital.data$Judge=="Keith"|capital.data$Judge=="Gregory"|capital.data$Judge=="Wynn"|capital.data$Judge=="Diaz"|capital.data$Judge=="Hull"|capital.data$Judge=="Marcus"|capital.data$Judge=="King"|capital.data$Judge=="Matheson"|capital.data$Judge=="J. Carnes"|capital.data$Judge=="Lucero"|capital.data$Judge=="Ginsburg"|capital.data$Judge=="Breyer"|capital.data$Judge=="Sotomayor"|capital.data$Judge=="Kagan"|capital.data$Judge=="Gould"|capital.data$Judge=="Shwartz"|capital.data$Judge=="Krause"|capital.data$Judge=="Clay"|capital.data$Judge=="Hood"|capital.data$Judge=="Martin"|capital.data$Judge=="J. Pryor"|capital.data$Judge=="Tallman"|capital.data$Judge=="Wilson"|capital.data$Judge=="Rosenbaum"|capital.data$Judge=="Ambro"|capital.data$Judge=="Greenaway"|capital.data$Judge=="Scirica"|capital.data$Judge=="Christen"|capital.data$Judge=="Nguyen"|capital.data$Judge=="Watford"|capital.data$Judge=="Rawlinson"|capital.data$Judge=="Vanaskie"|capital.data$Judge=="Kayatta"|capital.data$Judge=="Barron"|capital.data$Judge=="J. Kelly"|capital.data$Judge=="McKeown"|capital.data$Judge=="Stewart"|capital.data$Judge=="McKee"|capital.data$Judge=="Fuentes"|capital.data$Judge=="Silverman"|capital.data$Judge=="Moore"|capital.data$Judge=="Cole"|capital.data$Judge=="Schroeder"|capital.data$Judge=="Murguia"|capital.data$Judge=="Dennis"|capital.data$Judge=="Graves"|capital.data$Judge=="Fletcher"|capital.data$Judge=="Friedland"|capital.data$Judge=="Gilman"|capital.data$Judge=="Gilman"|capital.data$Judge=="Berzon"|capital.data$Judge=="Briscoe"|capital.data$Judge=="Motz"|capital.data$Judge=="DeGuilio"|capital.data$Judge=="Hornak"|capital.data$Judge=="Floyd"|capital.data$Judge=="Molloy"|capital.data$Judge=="Mendoza"|capital.data$Judge=="Schreier"|capital.data$Judge=="Higginson"|capital.data$Judge=="Merritt"|capital.data$Judge=="Traxler"|capital.data$Judge=="Thacker"|capital.data$Judge=="Thompson"|capital.data$Judge=="Wilkins"|capital.data$Judge=="Harris"|capital.data$Judge=="Pooler"|capital.data$Judge=="Lohier"|capital.data$Judge=="Rendell"|capital.data$Judge=="Bastian"|capital.data$Judge=="Stranch"|capital.data$Judge=="Graber"|capital.data$Judge=="Bennett"|capital.data$Judge=="Pearson"|capital.data$Judge=="Lynch"|capital.data$Judge=="Paez"|capital.data$Judge=="Reavley"|capital.data$Judge=="Beckwith"|capital.data$Judge=="Marmolejo"|capital.data$Judge=="Teilborg"|capital.data$Judge=="Hull"|capital.data$Judge=="Murphy"|capital.data$Judge=="Moritz"|capital.data$Judge=="Lipez"|capital.data$Judge=="Bacharach"|capital.data$Judge=="Hawkins"|capital.data$Judge=="Hurwitz"|capital.data$Judge=="Collins"|capital.data$Judge=="Gwin"|capital.data$Judge=="Sloviter"|capital.data$Judge=="Daughtry"|capital.data$Judge=="Leitman"|capital.data$Judge=="Calabresi"|capital.data$Judge=="Nelson"|capital.data$Judge=="Huck"|capital.data$Judge=="Adelman"|capital.data$Judge=="Bye"|capital.data$Judge=="Aldisert"|capital.data$Judge=="Timlin"|capital.data$Judge=="Sargus"|capital.data$Judge=="Hellerstein"|capital.data$Judge=="Barry"|capital.data$Judge=="Rice"|capital.data$Judge=="Collier"|capital.data$Judge=="Helmick"|capital.data$Judge=="Marbley"|capital.data$Judge=="Barkett"|capital.data$Judge=="Lemelle"|capital.data$Judge=="Canby"|capital.data$Judge=="Tashima"|capital.data$Judge=="O'Malley"|capital.data$Judge=="Cabranes"|capital.data$Judge=="Steeh"|capital.data$Judge=="Cudahy"|capital.data$Judge=="Carr"), 1, 0)

capital.data$race.party.interaction <- capital.data$D.s.Race*capital.data$Party


##################### Sentence Relief Rate Instead of Conviction Relief #####################
mean(ifelse(capital.data$Conviction==0 & capital.data$Sentence==1, 1, 0), na.rm =T)

######################################
######################################
##############################################################################
##############################################################################
################################################################################
#### Table 2 Regressions  (Table 1 is at the Bottom of this file) ################
################################################################################
##############################################################################
##############################################################################
##############################################################################
##############################################################################

## Column 1

OLS.regression <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Party + Type.of.Crime +  Number.of.Victims +  Jurisidiction  + race.party.interaction + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|0, data = capital.data)
summary(OLS.regression)

## Column 2

OLS.regression.judge.fixed <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year|Judge + State.of.Imprisonment|0|0, data = capital.data)
summary(OLS.regression.judge.fixed)

## Column 3
CSE.regression <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Party + Type.of.Crime +  Number.of.Victims +  Jurisidiction  + race.party.interaction + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|Case.Name, data = capital.data)
summary(CSE.regression)

## Column 4

CSE.regression.judge.fixed <- felm(Conviction ~ Sentence.at.Appeal +  D.s.Gender +  D.s.Race + Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year|Judge + State.of.Imprisonment|0|Case.Name, data = capital.data)
summary(CSE.regression.judge.fixed)

## Column 5

check.1 <- glm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Party + Type.of.Crime +  Number.of.Victims +  Jurisidiction  + race.party.interaction + Year + as.factor(Federal.Circuit.Court.Number), data=capital.data, family=binomial(link='logit'))

logit.model.cse.court.fixed <- coeftest(check.1, vcov = vcovHC(check.1, type = "HC1", cluster = capital.data$Case.Name))

## Column 6

check.2 <- glm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race + Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year + as.factor(Judge), data=capital.data, family=binomial(link='logit'))

logit.model.cse.judge.fixed <- coeftest(check.2, vcov = vcovHC(check.2, type = "HC1", cluster = capital.data$Case.Name))


## Testing the Significance of Variables Involving Party and Race (footnote 112)

q <- 20
df.denom <- 1357

CSE.regression.ftestrace <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  Party + Type.of.Crime +  Number.of.Victims +  Jurisidiction  + Year + Federal.Circuit.Court.Number.1 + Federal.Circuit.Court.Number.3 + Federal.Circuit.Court.Number.4 + Federal.Circuit.Court.Number.5 + Federal.Circuit.Court.Number.6 + Federal.Circuit.Court.Number.7 + Federal.Circuit.Court.Number.8 + Federal.Circuit.Court.Number.9 +  Federal.Circuit.Court.Number.10 +  Federal.Circuit.Court.Number.11 + Federal.Circuit.Court.Number.SCOTUS|0|0|Case.Name, data = capital.data)

CSE.regression.ftestparty <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Type.of.Crime +  Number.of.Victims +  Jurisidiction  + Year + Federal.Circuit.Court.Number.1 + Federal.Circuit.Court.Number.3 + Federal.Circuit.Court.Number.4 + Federal.Circuit.Court.Number.5 + Federal.Circuit.Court.Number.6 + Federal.Circuit.Court.Number.7 + Federal.Circuit.Court.Number.8 + Federal.Circuit.Court.Number.9 +  Federal.Circuit.Court.Number.10 +  Federal.Circuit.Court.Number.11 + Federal.Circuit.Court.Number.SCOTUS|0|0|Case.Name, data = capital.data)

SSR.ur <- sum((residuals(CSE.regression))^2, na.rm = T)
SSR.r <- sum((residuals(CSE.regression.ftestrace))^2, na.rm = T)

SSR.ur.party <- sum((residuals(CSE.regression))^2, na.rm = T)
SSR.r.party <- sum((residuals(CSE.regression.ftestparty))^2, na.rm = T)

### Getting the test statistic 

f.statistic.race.table.2 <- ((SSR.r-SSR.ur)/q)/(SSR.ur/(df.denom))
f.statistic.party.table.2 <- ((SSR.r.party-SSR.ur)/q)/(SSR.ur/(df.denom))

f.statistic.race.table.2

pf(f.statistic.race.table.2, q, df.denom, lower.tail=F)

pchisq(f.statistic.race.table.2*q, df.denom)
pchisq(f.statistic.party.table.2*q, df.denom)


### Producing the Latex Code for Table 2

covariate.names.nv <- c("Sentence at Time of Appeal (1 if Death, 0 Otherwise)", "Male (1 if Male, 0 Otherwise)", "Race (1 if White-Non-Hispanic, 0 Otherwise)", "Party of Appointing President (1 if Dem, 0 Otherwise)", "Type of Crime (1 if 1st Degree Murder or Equivalent, 0 Otherwise)", "Number of Victims", "Jurisdiction (1 if Federal, 0 Otherwise)", "Interaction of Race and Party", "Year")
dv.name.nv <- c("Guilt-Phase Vote (1 if Favorable to Defendant, 0 Otherwise)")

require("stargazer")
stargazer(OLS.regression, OLS.regression.judge.fixed, CSE.regression, CSE.regression.judge.fixed, logit.model.cse.court.fixed, logit.model.cse.judge.fixed, title="Table 2: Effects of Death Sentence on Probability of Favorable Habeas Ruling", align=T, type = "latex", report = ('vc*s'), multicolumn=T, covariate.labels = covariate.names.nv, omit=c("Federal.Circuit.Court.Number", "Judge", "State.of.Imprisonment", "Constant"), dep.var.labels = dv.name.nv, column.labels = c("OLS", "OLS", "CSE", "CSE", "Logit CSE", "Logit CSE"), column.separate = c(1,1), add.lines = list(c("Court-Level Fixed Effects", "Yes","No","Yes","No", "Yes","No"), c("Judge-Level Fixed Effects", "No","Yes","No","Yes","No","Yes"), c("State-Level Fixed Effects", "Yes","Yes","Yes","Yes","No","No"), c("Observations", "1,368","1,368","1,368","1,368","1,368","1,368")), omit.stat = c("adj.rsq", "f", "ser", "rsq", "n"), model.names = F, notes = "Interpretations: *p < .1; **p < .05; ***p < .01 ", notes.append = F, notes.align = "l", column.sep.width = "-15pt")

#########################################################################################
##### Calculating Probability Changes from Logit (Columns 5-6, Table 2) ####################
##########################################################################################

###### Treatment Effect ###### 

set.seed(2.223424231454878765)  
capital.data$constant <- 1
trimemed.capital.data <- capital.data[,c(49,23,4,3,6,22,21,12,48,18,11,5)]
trimemed.capital.data.all.death <- trimemed.capital.data
trimemed.capital.data.no.death <- trimemed.capital.data
trimemed.capital.data.all.death$Sentence.at.Appeal <- 1
trimemed.capital.data.no.death$Sentence.at.Appeal <- 0

# Court Fixed Effects (Column 5, Table 2)

treatment.effects <- vector()
i <- 1
for (s in 1:nrow(trimemed.capital.data)) {
  treatment.effects[i] <- predict(check.1, trimemed.capital.data.all.death[s,], type = "response") - predict(check.1, trimemed.capital.data.no.death[s,], type = "response")
  i <- i + 1
}


ate.court <- mean(treatment.effects, na.rm = T)

ate.court

# Judge Fixed Effects (Column 6, Table 2)

treatment.effects.judge <- vector()
i <- 1
for (s in 1:nrow(trimemed.capital.data)) {
  treatment.effects.judge[i] <- predict(check.2, trimemed.capital.data.all.death[s,], type = "response") - predict(check.1, trimemed.capital.data.no.death[s,], type = "response")
  i <- i + 1
}


ate.judge <- mean(treatment.effects.judge)

ate.judge

###### Calculating Standard Errors Via Multi-Variate Normal Simulation (Clarify) ######

prob.relief <- function(betas, xs){
  prob <- 1/(1+exp(-xs %*% betas))
  return(prob)
}


# Court Fixed Effects (Column 5, Table 2)

draw.coef <- mvrnorm(1000, mu = check.1$coefficients, Sigma = vcovHC(check.1, type = "HC1", cluster = capital.data$Case.Name))

capital.data$constant <- 1
capital.data.se.court <- capital.data[,c(49,23,4,3,6,22,21,12,48,18,44,45,36:43,46,47)]
capital.data.se.court.all.death <- capital.data.se.court
capital.data.se.court.no.death <- capital.data.se.court
capital.data.se.court.all.death$Sentence.at.Appeal <- 1
capital.data.se.court.no.death$Sentence.at.Appeal <- 0

simmed.ates <- c()
f <- 1
for (q in 1:1000) {
  sim.betas <- draw.coef[q,]
  treatment.effects.sim <- c()
  z <- 1
  for (s in 1:nrow(capital.data.se.court)) {
    treatment.effects.sim[z] <- prob.relief(sim.betas, as.matrix(capital.data.se.court.all.death[s,])) - prob.relief(sim.betas, as.matrix(capital.data.se.court.no.death[s,]))
    z <- z + 1
  }
  ate.loop <- mean(treatment.effects.sim)
  simmed.ates[f] <- ate.loop
  f <- f + 1
}

standard.error.court <- sd(simmed.ates)

standard.error.court

upper.bound <- ate.court + 1.96*standard.error.court
lower.bound <- ate.court - 1.96*standard.error.court
lower.bound
upper.bound 

################################################################
################################################################
##### Party Seperation (Table 3) ##########################################
##############################################################
################################################################

## Subsetting to Democratic and Republican Judges

capital.data.democratic.judges <- capital.data[capital.data$Party==1,]
capital.data.republican.judges <- capital.data[capital.data$Party==0,]

# Column 1

OLS.regression.democratic.judges <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|0, data = capital.data.democratic.judges)

summary(OLS.regression.democratic.judges)

# Column 2

CSE.regression.democratic.judges <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|Case.Name, data = capital.data.democratic.judges)

summary(CSE.regression.democratic.judges)

# Column 3

check.3 <- glm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year + as.factor(Federal.Circuit.Court.Number), data=capital.data.democratic.judges, family=binomial(link='logit'))

logit.model.dem <- coeftest(check.3, vcov = vcovHC(check.3, type = "HC1", cluster = capital.data.democratic.judges$Case.Name))

# Column 4

OLS.regression.rep.judges <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|0, data = capital.data.republican.judges)
summary(OLS.regression.rep.judges)

# Column 5

CSE.regression.rep.judges <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|Case.Name, data = capital.data.republican.judges)

summary(CSE.regression.rep.judges)

# Column 6

check.4 <- glm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year + as.factor(Federal.Circuit.Court.Number), data=capital.data.republican.judges, family=binomial(link='logit'))

logit.model.rep <- coeftest(check.4, vcov = vcovHC(check.4, type = "HC1", cluster = capital.data.republican.judges$Case.Name))



### Latex Code for Table 3 ###

covariate.names.nv.2 <- c("Sentence at Time of Appeal (1 if Death, 0 Otherwise)", "Male (1 if Male, 0 Otherwise)", "Race (1 if White-Non-Hispanic, 0 Otherwise)", "Type of Crime (1 if 1st Degree Murder or Equivalent, 0 Otherwise)", "Number of Victims", "Jurisdiction (1 if Federal, 0 Otherwise)", "Year")
dv.name.nv.2 <- c("Guilt-Phase Vote", "Guilt-Phase Vote")

require("stargazer")

stargazer(OLS.regression.democratic.judges, CSE.regression.democratic.judges, logit.model.dem, OLS.regression.rep.judges, CSE.regression.rep.judges, logit.model.rep, title="Table 3: Effects of Death Sentence on Probability of Favorable Habeas Ruling—Effects Seperated By Party", align=TRUE, type = "latex", report = ('vc*s'), multicolumn=T, covariate.labels = covariate.names.nv.2, omit=c("Federal.Circuit.Court.Number", "State.of.Imprisonment", "Constant"), dep.var.labels = c(" ", "Guilt-Phase Vote", " ", " ", "Guilt-Phase Vote", " "), column.labels = c("Democratic Judges", "Republican Judges"), column.separate = c(3,3), add.lines = list(c("Court-Level Fixed Effects", "Yes","Yes", "Yes", "Yes", "Yes", "Yes"), c("State-Level Fixed Effects", "Yes","Yes", "No", "Yes", "Yes", "No"), c("Standard Errors", "OLS", "CSE", "Logit", "OLS", "CSE", "Logit"), c("Observations", "686", "690", "686", "686", "682", "682")), omit.stat = c("adj.rsq", "f", "ser", "n", "rsq"), model.names = F, column.sep.width = "-15pt", notes = "Interpretations: *p < .1; **p < .05; ***p < .01 ", notes.append = F, notes.align = "l")

## General Rates of Relief Under Democratic and Republican Judges

mean(capital.data.democratic.judges$Conviction, na.rm = T)
mean(capital.data.republican.judges$Conviction, na.rm = T)

#########################################################################################
##### Calculating Probability Changes from Logit (Columns 3, 6, Table 4) ####################
##########################################################################################

###### Treatment Effect ###### 

## Column 3

capital.data.democratic.judges$constant <- 1
trimemed.capital.data.democratic.judges <- capital.data.democratic.judges[,c(49,23,4,3,6,22,21,12,48,18,11,5)]
trimemed.capital.data.democratic.judges.all.death <- trimemed.capital.data.democratic.judges
trimemed.capital.data.democratic.judges.no.death <- trimemed.capital.data.democratic.judges
trimemed.capital.data.democratic.judges.all.death$Sentence.at.Appeal <- 1
trimemed.capital.data.democratic.judges.no.death$Sentence.at.Appeal <- 0

treatment.effects.dem <- vector()
i <- 1
for (s in 1:nrow(trimemed.capital.data.democratic.judges.all.death)) {
  treatment.effects.dem[i] <- predict(check.3, trimemed.capital.data.democratic.judges.all.death[s,], type = "response") - predict(check.3, trimemed.capital.data.democratic.judges.no.death[s,], type = "response")
  i <- i + 1
}

ate.court.dem <- mean(treatment.effects.dem)

ate.court.dem

## Column 6 

capital.data.republican.judges$constant <- 1
trimemed.capital.data.republican.judges <- capital.data.republican.judges[,c(49,23,4,3,6,22,21,12,48,18,11,5)]
trimemed.capital.data.republican.judges.all.death <- trimemed.capital.data.republican.judges
trimemed.capital.data.republican.judges.no.death <- trimemed.capital.data.republican.judges
trimemed.capital.data.republican.judges.all.death$Sentence.at.Appeal <- 1
trimemed.capital.data.republican.judges.no.death$Sentence.at.Appeal <- 0

treatment.effects.rep <- vector()
i <- 1
for (s in 1:nrow(trimemed.capital.data.republican.judges.all.death)) {
  treatment.effects.rep[i] <- predict(check.4, trimemed.capital.data.republican.judges.all.death[s,], type = "response") - predict(check.4, trimemed.capital.data.republican.judges.no.death[s,], type = "response")
  i <- i + 1
}

ate.court.rep <- mean(treatment.effects.rep)

ate.court.rep

###### Calculating Standard Errors Via Multi-Variate Normal Simulation (Clarify) ######

## Column 5

draw.coef.dems <- mvrnorm(1000, mu = check.3$coefficients, Sigma = vcovHC(check.3, type = "HC1", cluster = capital.data.democratic.judges$Case.Name))

capital.data.democratic.judges$constant <- 1
capital.data.se.court.dems <- capital.data.democratic.judges[,c(49,23,4,3,22,21,12,18,44,45,36:43,46,47)]
capital.data.se.court.all.death.dems <- capital.data.se.court.dems
capital.data.se.court.no.death.dems <- capital.data.se.court.dems
capital.data.se.court.all.death.dems$Sentence.at.Appeal <- 1
capital.data.se.court.no.death.dems$Sentence.at.Appeal <- 0

simmed.ates.dems <- vector()
f <- 1
for (q in 1:1000) {
  sim.betas <- draw.coef.dems[q,]
  treatment.effects.sim <- vector()
  z <- 1
  for (s in 1:nrow(capital.data.se.court.dems)) {
    treatment.effects.sim[z] <- prob.relief(sim.betas, as.matrix(capital.data.se.court.all.death.dems[s,])) - prob.relief(sim.betas, as.matrix(capital.data.se.court.no.death.dems[s,]))
    z <- z + 1
  }
  ate.loop <- mean(treatment.effects.sim)
  simmed.ates.dems[f] <- ate.loop
  f <- f + 1
}

standard.error.dem <- sd(simmed.ates.dems)

standard.error.dem

upper.bound.dem <- ate.court.dem + 1.96*standard.error.dem
lower.bound.dem <- ate.court.dem - 1.96*standard.error.dem
lower.bound.dem
upper.bound.dem

2*pt(-(ate.court.dem/standard.error.dem), 670)

draw.coef.reps <- mvrnorm(1000, mu = check.3$coefficients, Sigma = vcovHC(check.3, type = "HC1", cluster = capital.data.republican.judges$Case.Name))

capital.data.republican.judges$constant <- 1
capital.data.se.court.reps <- capital.data.republican.judges[,c(49,23,4,3,22,21,12,18,44,45,36:43,46,47)]
capital.data.se.court.all.death.reps <- capital.data.se.court.reps
capital.data.se.court.no.death.reps <- capital.data.se.court.reps
capital.data.se.court.all.death.reps$Sentence.at.Appeal <- 1
capital.data.se.court.no.death.reps$Sentence.at.Appeal <- 0

## Column 6

simmed.ates.reps <- vector()
f <- 1
for (q in 1:1000) {
  sim.betas <- draw.coef.reps[q,]
  treatment.effects.sim <- vector()
  z <- 1
  for (s in 1:nrow(capital.data.se.court.reps)) {
    treatment.effects.sim[z] <- prob.relief(sim.betas, as.matrix(capital.data.se.court.all.death.reps[s,])) - prob.relief(sim.betas, as.matrix(capital.data.se.court.no.death.reps[s,]))
    z <- z + 1
  }
  ate.loop <- mean(treatment.effects.sim)
  simmed.ates.reps[f] <- ate.loop
  f <- f + 1
}

standard.error.rep <- sd(simmed.ates.reps)

standard.error.rep

upper.bound.rep <- ate.court.rep + 1.96*standard.error.rep
lower.bound.rep <- ate.court.rep - 1.96*standard.error.rep
lower.bound.rep
upper.bound.rep

2*pt(-(ate.court.rep/standard.error.rep), 667)


###########################################################################
######################################################################################
####### First Degree Murder or Equivalent Only (Table 4) ##############################
#####################################################################################
###########################################################################


## Subsetting to First Degree Murder Cases

capital.data.fdm <- capital.data[capital.data$Type.of.Crime==1,]

## Column 1

OLS.regression.fdm <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Party + Number.of.Victims +  Jurisidiction  + race.party.interaction + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|0, data = capital.data.fdm)
summary(OLS.regression.fdm)

## Column 2

OLS.regression.fdm.judge.fixed <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race + Number.of.Victims +  Jurisidiction  + Year|Judge + State.of.Imprisonment|0|0, data = capital.data.fdm)
summary(OLS.regression.fdm.judge.fixed)

## Column 3

CSE.regression.fdm <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Party + Number.of.Victims +  Jurisidiction  + race.party.interaction + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|Case.Name, data = capital.data.fdm)
summary(CSE.regression.fdm)

## Column 4

CSE.regression.fdm.judge.fixed <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race + Number.of.Victims +  Jurisidiction  + Year|Judge + State.of.Imprisonment|0|Case.Name, data = capital.data.fdm)
summary(CSE.regression.fdm.judge.fixed)

## Column 5

check.5 <- glm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Party + Number.of.Victims +  Jurisidiction  + race.party.interaction + Year + as.factor(Federal.Circuit.Court.Number), data=capital.data.fdm, family=binomial(link='logit'))

logit.model.cse.court.fixed.fdm <- coeftest(check.5, vcov = vcovHC(check.5, type = "HC1", cluster = capital.data.fdm$Case.Name))

## Observing Problem of Perfect/Near Perfect Seperation in footnote 113

check.6 <- glm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Party + Number.of.Victims +  Jurisidiction  + race.party.interaction + Year + as.factor(Judge), data=capital.data.fdm, family=binomial(link='logit'))

### Producing the Latex Code for Table 4

covariate.names.nv.3 <- c("Sentence at Time of Appeal (1 if Death, 0 Otherwise)", "Male (1 if Male, 0 Otherwise)", "Race (1 if White-Non-Hispanic, 0 Otherwise)", "Party of Appointing President (1 if Dem, 0 Otherwise)", "Number of Victims", "Jurisdiction (1 if Federal, 0 Otherwise)", "Interaction of Race and Party", "Year")
dv.name.nv <- c("Guilt-Phase Vote (1 if Favorable to Defendant, 0 Otherwise)")

require("stargazer")
stargazer(OLS.regression.fdm, OLS.regression.fdm.judge.fixed, CSE.regression.fdm, CSE.regression.fdm.judge.fixed, logit.model.cse.court.fixed.fdm, title="Table 4: Effects of Death Sentence on Probability of Favorable Habeas Ruling — First Degree Murder and Equivalents Only", align=TRUE, type = "latex", report = ('vc*s'), multicolumn=T, covariate.labels = covariate.names.nv.3, omit=c("Constant", "Federal.Circuit.Court.Number", "Judge", "State.of.Imprisonment"), dep.var.labels = dv.name.nv, column.labels = c("OLS","CSE", "Logit"), column.separate = c(2,2,1), add.lines = list(c("Court-Level Fixed Effects", "Yes","No","Yes","No", "Yes"), c("Judge-Level Fixed Effects", "No","Yes","No","Yes","No"), c("State-Level Fixed Effects", "Yes","Yes","Yes","Yes", "No"), c("Observations", "1,216", "1,216", "1,216", "1,216", "1,216") ), omit.stat = c("adj.rsq", "f", "ser", "rsq", "n"), model.names = F, notes = "Interpretations: *p < .1; **p < .05; ***p < .01 ", notes.append = F, notes.align = "l")

#########################################################################################
##### Calculating Probability Changes from Logit (Columns 5, Table 3) ####################
##########################################################################################


## Effect Size 

capital.data.fdm$constant <- 1
trimemed.capital.data.fdm <- capital.data.fdm[,c(49,23,4,3,6,22,21,12,48,18,11,5)]
trimemed.capital.data.fdm.all.death <- trimemed.capital.data.fdm
trimemed.capital.data.fdm.no.death <- trimemed.capital.data.fdm
trimemed.capital.data.fdm.all.death$Sentence.at.Appeal <- 1
trimemed.capital.data.fdm.no.death$Sentence.at.Appeal <- 0

treatment.effects.fdm <- vector()
i <- 1
for (s in 1:nrow(trimemed.capital.data.fdm.all.death)) {
  treatment.effects.fdm[i] <- predict(check.5, trimemed.capital.data.fdm.all.death[s,], type = "response") - predict(check.5, trimemed.capital.data.fdm.no.death[s,], type = "response")
  i <- i + 1
}

ate.court.fdm <- mean(treatment.effects.fdm)

ate.court.fdm

## Calculating Standard Errors Via Multi-Variate Normal Simulation (Clarify) ##

draw.coef.fdm <- mvrnorm(1000, mu = check.5$coefficients, Sigma = vcovHC(check.5, type = "HC1", cluster = capital.data.fdm$Case.Name))

capital.data.fdm$constant <- 1
capital.data.se.court.fdm <- capital.data.fdm[,c(49,23,4,3,22,21,12,48,18,44,45,36:43,47)]
capital.data.se.court.all.death.fdm <- capital.data.se.court.fdm
capital.data.se.court.no.death.fdm <- capital.data.se.court.fdm
capital.data.se.court.all.death.fdm$Sentence.at.Appeal <- 1
capital.data.se.court.no.death.fdm$Sentence.at.Appeal <- 0

simmed.ates.fdm <- vector()
f <- 1
for (q in 1:1000) {
  sim.betas <- draw.coef.fdm[q,]
  treatment.effects.sim <- vector()
  z <- 1
  for (s in 1:nrow(capital.data.se.court.fdm)) {
    treatment.effects.sim[z] <- prob.relief(sim.betas, as.matrix(capital.data.se.court.all.death.fdm[s,])) - prob.relief(sim.betas, as.matrix(capital.data.se.court.no.death.fdm[s,]))
    z <- z + 1
  }
  ate.loop <- mean(treatment.effects.sim)
  simmed.ates.fdm[f] <- ate.loop
  f <- f + 1
}

standard.error.fdm <- sd(simmed.ates.fdm)

standard.error.fdm

upper.bound.fdm <- ate.court.fdm + 1.96*standard.error.fdm
lower.bound.fdm <- ate.court.fdm - 1.96*standard.error.fdm
lower.bound.fdm
upper.bound.fdm

2*pt(-(ate.court.fdm/standard.error.fdm), 670)


##### Testing Claim in Footnote 112 #####

capital.data.fdm.race <- capital.data.fdm[capital.data.fdm$D.s.Race==0,]

CSE.regression.race <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  Party + Number.of.Victims +  Jurisidiction  + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|Case.Name, data = capital.data.fdm.race)

summary(CSE.regression.race)

CSE.regression.race.judge.fixed <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender + Number.of.Victims +  Jurisidiction  + Year|Judge + State.of.Imprisonment|0|Case.Name, data = capital.data.fdm.race)

summary(CSE.regression.race)

############################################################
###### Differential Investment (Table 5) ###################################
############################################################

##############################
### Subsetting the Data ######
##############################

capital.data$multiple.defense <- ifelse(capital.data$n.defendent.attorneys>1, 1, 0)
capital.data.high.investment <- capital.data[capital.data$multiple.defense==1,]
capital.data.high.profile <- capital.data[capital.data$en.banc==1 & capital.data$Federal.Circuit.Court.Number.SCOTUS==0,]
capital.data.public.attorney <- capital.data[capital.data$d.public==1,]

# Column 1

CSE.regression.circuit.fixed.mutliple.attorneys <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Party + Type.of.Crime +  Number.of.Victims +  Jurisidiction  + race.party.interaction + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|Case.Name, data = capital.data.high.investment)
summary(CSE.regression.circuit.fixed.mutliple.attorneys)

# Column 2

CSE.regression.judge.fixed.mutliple.attorneys <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race + Type.of.Crime +  Number.of.Victims +  Jurisidiction  + Year|Judge + State.of.Imprisonment|0|Case.Name, data = capital.data.high.investment)
summary(CSE.regression.judge.fixed.mutliple.attorneys)

# Column 3

CSE.regression.high.profile <- felm(Conviction ~  Sentence.at.Appeal + D.s.Race +  Party + Type.of.Crime +  Number.of.Victims +  race.party.interaction + Year|Federal.Circuit.Court.Number|0|Case.Name, data = capital.data.high.profile)
summary(CSE.regression.high.profile)

# Column 4

CSE.regression.circuit.fixed.public.attorney <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Party + Type.of.Crime +  Number.of.Victims +  Jurisidiction  + race.party.interaction + Year|Federal.Circuit.Court.Number + State.of.Imprisonment|0|Case.Name, data = capital.data.public.attorney)
summary(CSE.regression.circuit.fixed.public.attorney)

# Column 5

CSE.regression.judge.fixed.public.attorney <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  D.s.Race + Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year|Judge + State.of.Imprisonment|0|Case.Name, data = capital.data.public.attorney)
summary(CSE.regression.judge.fixed.public.attorney)


### Producing the Latex Code for Table 5


covariate.names.nv <- c("Sentence at Time of Appeal (1 if Death, 0 Otherwise)", "Male (1 if Male, 0 Otherwise)", "Race (1 if White-Non-Hispanic, 0 Otherwise)", "Party of Appointing President (1 if Dem, 0 Otherwise)", "Type of Crime (1 if 1st Degree Murder or Equivalent, 0 Otherwise)", "Number of Victims", "Jurisdiction (1 if Federal, 0 Otherwise)", "Interaction of Race and Party", "Year")
dv.name.nv <- c("Guilt-Phase Vote (1 if Favorable to Defendant, 0 Otherwise)")

stargazer(CSE.regression.circuit.fixed.mutliple.attorneys, CSE.regression.judge.fixed.mutliple.attorneys, CSE.regression.high.profile, CSE.regression.circuit.fixed.public.attorney, CSE.regression.judge.fixed.public.attorney, title="Table 5: Sentencing Effect, Controlling for Investment Level", align=T, type = "latex", report = ('vc*s'), multicolumn=T, covariate.labels = covariate.names.nv, omit=c("Constant"), dep.var.labels = dv.name.nv, column.labels = c("Multiple Defense Attorneys", "En Banc", "Public Defender"), column.separate = c(2,1,2), add.lines = list(c("Court-Level Fixed Effects", "Yes","No","Yes", "Yes", "No"), c("Judge-Level Fixed Effects", "No","Yes", "No", "No", "Yes"), c("Court-Level Fixed Effects", "Yes","Yes","No", "Yes", "Yes"),  c("Observations", "726","726","94", "368", "368")), omit.stat = c("adj.rsq", "f", "ser"), model.names = F, notes = c("Interpretations: *p < .1; **p < .05; ***p < .01.", "All Standard Errors Clustered By Case."), notes.append = F, notes.align = "r", column.sep.width = "-15pt")

#########################################################
##### Ordered Logit Test (Tables 6-7) ###############
#########################################################

### Estimating Mean Increase in Probability ###

# (1) Running the Regression

set.seed(12)

capital.data.ordered <- capital.data[complete.cases(capital.data[ , c(32)]),]

capital.data.ordered <- capital.data.ordered[capital.data.ordered$Federal.Circuit.Court.Number!="D.C.",]

capital.data.ordered$Conviction.original <- as.factor(capital.data.ordered$Conviction.original)


ologit1 <- polr(factor(Conviction.original, ordered=T) ~ Sentence.at.Appeal +  D.s.Gender +  D.s.Race +  Party + Type.of.Crime +  Number.of.Victims +  Jurisidiction  + race.party.interaction + Year + factor(Federal.Circuit.Court.Number), data = capital.data.ordered, method = "logistic", Hess=TRUE)

summary(ologit1)

### Cutoff Values

ologit1$zeta

### Producing the Latex Code for Table A.1

covariate.names.nv <- c("Sentence at Time of Appeal", "Male (1 if Male, 0 Otherwise)", "Race (1 if White-Non-Hispanic, 0 Otherwise)", "Party of Appointing President (1 if Dem, 0 Otherwise)", "Type of Crime (1 if 1st Degree Murder or Equivalent, 0 Otherwise)", "Number of Victims", "Jurisdiction (1 if Federal, 0 Otherwise)", "Interaction of Race and Party", "Year")
dv.name.nv <- c("Guilt-Phase Outcome")

stargazer(ologit1, title="Table A.1: Ordered Logit Estimates of Sentencing Effect", type = "latex", report = ('vc*s'), covariate.labels = covariate.names.nv, dep.var.labels = dv.name.nv, omit=c("Federal.Circuit.Court.Number", "Constant"), notes = c("Possible Outcomes (in order):", "(1) No Relief", "(2) Partial Relief",  "(3) Habeas Granted", "", "Psi Values: 73.76291, 74.71697"), notes.align = "r", notes.append = F, add.lines = list(c("Court-Level Fixed Effects", "Yes")))

# (2) Function for Calculating Mean Probability Increase

prob.of.category <- function(psij, psij.1, betas, xs) {
  prob.j <- (exp(psij - xs %*% betas)/(1+exp(psij - xs %*% betas))) - (exp(psij.1 - xs %*% betas)/(1+exp(psij.1 - xs %*% betas)))
  return(prob.j)
}

# Preliminary Preparation

betas <- ologit1$coefficients

sub.df <- capital.data.ordered[,c(23, 4,3,6, 22, 21, 12, 48, 18, 44:45, 36:43, 47)]

all.death <-  sub.df
no.death <-  sub.df
all.death$Sentence.at.Appeal <-  1
no.death$Sentence.at.Appeal <-  0

# (2) Looping Through Data to Assessing Mean Change Probability of Falling in Each Category given that sentence goes from 0 to 1.

no.relief <- vector()
i <- 1
for (s in 1:nrow(all.death)) {
  no.relief[i] <- prob.of.category(73.76291, -1000, as.matrix(betas), as.matrix(all.death[s,])) - prob.of.category(73.76291, -1000, as.matrix(betas), as.matrix(no.death[s,]))
  i <- i + 1
}

no.relief.effect <- mean(no.relief)

no.relief.effect

some.relief <- vector()
i <- 1
for (s in 1:nrow(all.death)) {
  some.relief[i] <- prob.of.category(74.71697, 73.76291, as.matrix(betas), as.matrix(all.death[s,])) - prob.of.category(74.71697, 73.76291, as.matrix(betas), as.matrix(no.death[s,]))
  i <- i + 1
}

some.relief.effect <- mean(some.relief)

some.relief.effect

complete.relief <- vector()
i <- 1
for (s in 1:nrow(all.death)) {
  complete.relief[i] <- prob.of.category(400, 74.71697, as.matrix(betas), as.matrix(all.death[s,])) - prob.of.category(400, 74.71697, as.matrix(betas), as.matrix(no.death[s,]))
  i <- i + 1
}

complete.relief.effect <- mean(complete.relief)

complete.relief.effect


###### MVN Simulation for Confidence Intervals for Increases in Probabilities #####

psi.values <- ologit1$zeta
names(psi.values) <- c("psi.1", "psi.2")

draw.coef <- mvrnorm(1000, mu = c(ologit1$coefficients, psi.values), Sigma = vcovCL(ologit1, cluster = capital.data.ordered$Case.Name))
draw.coef <- as.data.frame(draw.coef)
independent.variables <- draw.coef[,c(1:20)]
bootstrap.frame <- capital.data.ordered[,c(23, 4, 3, 6, 22, 21, 12, 48, 18, 44:45, 36:43, 47)]
complete.relief.values <- vector()
for (q in 1:1000) {
  sub.df.boot <- bootstrap.frame
  all.death.boot <-  sub.df.boot
  no.death.boot <-  sub.df.boot
  all.death.boot$Sentence.at.Appeal <-  1
  no.death.boot$Sentence.at.Appeal <-  0
  betas.boot <- independent.variables[q,]
  complete.relief.boot <- vector()
  k <- 1
  for (s in 1:nrow(all.death.boot)) {
    complete.relief.boot[k] <- prob.of.category(400, draw.coef$psi.2[q], t(as.matrix(betas.boot)), as.matrix(all.death.boot[s,])) - prob.of.category(400, draw.coef$psi.2[q], t(as.matrix(betas.boot)), as.matrix(no.death.boot[s,]))
    k <- k + 1
  }
  complete.relief.effect.boot <- mean(complete.relief.boot)
  complete.relief.values[q] <- complete.relief.effect.boot
  
}

sd(complete.relief.values)


draw.coef <- mvrnorm(1000, mu = c(ologit1$coefficients, psi.values), Sigma = vcovCL(ologit1, cluster = capital.data.ordered$Case.Name))
draw.coef <- as.data.frame(draw.coef)
independent.variables <- draw.coef[,c(1:20)]
bootstrap.frame <- capital.data.ordered[,c(23, 4, 3, 6, 22, 21, 12, 48, 18, 44:45, 36:43, 47)]
partial.relief.values <- vector()
for (q in 1:1000) {
  sub.df.boot <- bootstrap.frame
  all.death.boot <-  sub.df.boot
  no.death.boot <-  sub.df.boot
  all.death.boot$Sentence.at.Appeal <-  1
  no.death.boot$Sentence.at.Appeal <-  0
  betas.boot <- independent.variables[q,]
  partial.relief.boot <- vector()
  k <- 1
  for (s in 1:nrow(all.death.boot)) {
    partial.relief.boot[k] <- prob.of.category(draw.coef$psi.2[q], draw.coef$psi.1[q], t(as.matrix(betas.boot)), as.matrix(all.death.boot[s,])) - prob.of.category(draw.coef$psi.2[q], draw.coef$psi.2[1], t(as.matrix(betas.boot)), as.matrix(no.death.boot[s,]))
    k <- k + 1
  }
  partial.relief.effect <- mean(partial.relief.boot)
  partial.relief.values[q] <- partial.relief.effect
  
}

sd(partial.relief.values)


draw.coef <- mvrnorm(1000, mu = c(ologit1$coefficients, psi.values), Sigma = vcovCL(ologit1, cluster = capital.data.ordered$Case.Name))
draw.coef <- as.data.frame(draw.coef)
independent.variables <- draw.coef[,c(1:20)]
bootstrap.frame <- capital.data.ordered[,c(23, 4, 3, 6, 22, 21, 12, 48, 18, 44:45, 36:43, 47)]
no.relief.values <- vector()
for (q in 1:1000) {
  sub.df.boot <- bootstrap.frame
  all.death.boot <-  sub.df.boot
  no.death.boot <-  sub.df.boot
  all.death.boot$Sentence.at.Appeal <-  1
  no.death.boot$Sentence.at.Appeal <-  0
  betas.boot <- independent.variables[q,]
  no.relief.boot <- vector()
  k <- 1
  for (s in 1:nrow(all.death.boot)) {
    no.relief.boot[k] <- prob.of.category(draw.coef$psi.1[q], -1000, t(as.matrix(betas.boot)), as.matrix(all.death.boot[s,])) - prob.of.category(draw.coef$psi.2[q], draw.coef$psi.2[1], t(as.matrix(betas.boot)), as.matrix(no.death.boot[s,]))
    k <- k + 1
  }
  no.relief.effect <- mean(no.relief.boot)
  no.relief.values[q] <- no.relief.effect
  
}

sd(no.relief.values)

2*pt(-abs(no.relief.effect/sd(no.relief.values)), df=1346)
2*pt(-abs(some.relief.effect/sd(partial.relief.values)), df=1346)
2*pt(-abs(complete.relief.effect/sd(complete.relief.values)), df=1346)

sum.stats.y <- matrix(nrow = 2, ncol = 4)
sum.stats.y[1,] <- c("Effect Size (Percentage Points)", "-8.16**", "3.28***", "4.89**")
sum.stats.y[2,] <- c("Standard Error", "(3.26)", "(1.20)", "(2.04)")


### Producing the Latex Code for Table 7

sum.stats <- as.data.frame(sum.stats.y)
library(knitr)
library("kableExtra")
y <- kable(sum.stats.y, col.names = c(" ", "Increase in Probability of No Relief", "Increase in Probability of Partial Relief", "Increasing in Probability of Granting Habeas"), format = "latex", align = "c", booktabs = TRUE) %>% kable_styling(latex_options = c("scale_down", "basic"), font_size = 8, full_width = F) %>% row_spec(0, bold = T) %>% footnote(general = "N = 1,368", footnote_as_chunk = T, general_title = " ") %>% footnote(general = "Standard Erros Calculated Using Mulitvariate Normal Simulations", footnote_as_chunk = T, general_title = " ") %>% footnote(general = "Interpretations: *p < .1; **p < .05; ***p < .01", footnote_as_chunk = T, general_title = " ")

y <- add_header_above(y, c("Table a: Effect Sizes From Ordered Logit" = 4), bold = T, underline = F, font_size = 10) 

y <- column_spec(y, 1, width = "5cm", bold = T)

y <- column_spec(y, 2, width = "3cm", bold = T)

y <- column_spec(y, 3, width = "3cm", bold = T)

y <- column_spec(y, 4, width = "3cm", bold = T)

y

######################################################
######################################################
######################################################
######################################################
###### Matching (Tables 7 and A.2, Figure A.1) ##############
######################################################
######################################################
######################################################
########################

#####################################
### Prospensity Matching ###########
#####################################

# Estiamting Propensity Scores
set.seed(2.223424231454878765)  

capital.data.na.removed.investment <- capital.data[complete.cases(capital.data[ , c(15,23)]),]

prop.logit <- glm(Sentence.at.Appeal ~ D.s.Gender +  D.s.Race +  Type.of.Crime.original +  Number.of.Victims +  Jurisidiction + State.of.Imprisonment, family=binomial(link="logit"), data = capital.data.na.removed.investment)

capital.data.na.removed.investment$est.prop <- prop.logit$fitted.values

hist(capital.data.na.removed.investment$est.prop, freq = F, breaks = 50, col = "black")

## Graphing Overlap (Figure 1)

library(ggplot2)
ggplot(data = capital.data.na.removed.investment)+
  geom_density(aes(x = est.prop, fill = as.factor(Sentence.at.Appeal)), alpha = 0.4)+
  scale_fill_manual(values = c("0" = "gray20", "1" = "gray60"), 
                    name = "Captial Sentence") +
  theme_classic()+
  xlab("Propensity")+
  ylab("Density")+
  ggtitle("Figure A.1: Propensity Score Overlap") +
  theme(plot.title = element_text(color="black", size=20, face="bold", hjust = 0.5), axis.title.x = element_text(color="black", size=16, face="bold"), axis.title.y = element_text(color="black", size=16, face="bold"), axis.text.x = element_text(color="black", size=14, face="bold"), axis.text.y = element_text(color="black", size=14, face="bold"))


##### Limit to Propensity Scores Between .1 and .9 (see Crump, et al. (2006))

capital.data.na.removed.investment.narrowed <- capital.data.na.removed.investment[(capital.data.na.removed.investment$est.prop>= .1 & capital.data.na.removed.investment$est.prop<= .9),]

matchoutput.w.replacement.narrowed <- Match(Y = capital.data.na.removed.investment.narrowed$Conviction, Tr = capital.data.na.removed.investment.narrowed$Sentence.at.Appeal, X = capital.data.na.removed.investment.narrowed$est.prop, M = 1, replace = T, estimand = "ATE", BiasAdjust = TRUE)

matchoutput.wo.replacement.narrowed <- Match(Y = capital.data.na.removed.investment.narrowed$Conviction, Tr = capital.data.na.removed.investment.narrowed$Sentence.at.Appeal, X = capital.data.na.removed.investment.narrowed$est.prop, M = 1, replace = F, estimand = "ATE", BiasAdjust = TRUE)

summary.Match(matchoutput.wo.replacement.narrowed)
summary.Match(matchoutput.w.replacement.narrowed)

# (1) Covariate Balance With Replacement

x.vars <- c("D.s.Gender",  "D.s.Race",  "Number.of.Victims",  "Jurisidiction")

mb.all.w.replacement.narrowed <- MatchBalance(Sentence.at.Appeal ~ D.s.Gender +  D.s.Race +  Number.of.Victims +  Jurisidiction, data=capital.data.na.removed.investment, match.out = matchoutput.w.replacement.narrowed)

out.after.match.all.w.replacement.narrowed <- baltest.collect(matchbal.out = mb.all.w.replacement.narrowed, var.names = x.vars, after=TRUE)

# (2) Covariate Balance Without Replacement

mb.all.wo.replacement.narrowed <- MatchBalance(Sentence.at.Appeal ~ D.s.Gender +  D.s.Race +  Number.of.Victims +  Jurisidiction, data=capital.data.na.removed.investment, match.out = matchoutput.wo.replacement.narrowed)

out.after.match.all.wo.replacement.narrowed <- baltest.collect(matchbal.out = mb.all.wo.replacement.narrowed, var.names = x.vars, after=TRUE)

#############################################
##### Allowing all Propensity Scores ##########
#############################################

# Estimate

matchoutput.w.replacement <- Match(Y = capital.data.na.removed.investment$Conviction, Tr = capital.data.na.removed.investment$Sentence.at.Appeal, X = capital.data.na.removed.investment$est.prop, M = 1, replace = T, estimand = "ATE", BiasAdjust = TRUE)

summary(matchoutput.w.replacement)

matchoutput.wo.replacement <- Match(Y = capital.data.na.removed.investment$Conviction, Tr = capital.data.na.removed.investment$Sentence.at.Appeal, X = capital.data.na.removed.investment$est.prop, M = 1, replace = F, estimand = "ATE", BiasAdjust = TRUE)

summary(matchoutput.wo.replacement)

# (1) Covariate Balance With Replacement

x.vars <- c("D.s.Gender",  "D.s.Race",  "Number.of.Victims",  "Jurisidiction")

mb.all.w.replacement <- MatchBalance(Sentence.at.Appeal ~ D.s.Gender +  D.s.Race +  Number.of.Victims +  Jurisidiction, data=capital.data.na.removed.investment, match.out = matchoutput.w.replacement)

out.after.match.all.w.replacement <- baltest.collect(matchbal.out = mb.all.w.replacement, var.names = x.vars, after=TRUE)

# (2) Covariate Balance Without Replacement

mb.all.wo.replacement <- MatchBalance(Sentence.at.Appeal ~ D.s.Gender +  D.s.Race +  Number.of.Victims +  Jurisidiction, data=capital.data.na.removed.investment, match.out = matchoutput.wo.replacement)

out.after.match.all.wo.replacement <- baltest.collect(matchbal.out = mb.all.wo.replacement, var.names = x.vars, after=TRUE)


#### ### Producing the Latex Code for Table 7

sum.stats <- matrix(nrow = 3, ncol = 5)
sum.stats[1,] <- c("ATE/ATT", "0.016", "0.342***", "0.053**", "0.126***")
sum.stats[2,] <- c("Standard Errors", "(0.016)", "(0.082)", "(0.022)", "(0.036)")
sum.stats[3,] <- c("Matched # of Observations", "1258", "1368", "692", "829")

sum.stats <- as.data.frame(sum.stats)
library(knitr)
library("kableExtra")
c <- kable(sum.stats, col.names = c("Quantity", "Without Replacement", "With Replacement", "Without Replacement", "With Replacement"), format = "latex", booktabs = T, align = "c") %>% kable_styling(font_size = 8, full_width = F) %>% footnote(general = "Estimates using replacement are of the Average Treatment Effect (ATE).", footnote_as_chunk = T, general_title = " ") %>% footnote(general = "Estimates not using replacement are of the Average Treatment Effect on the Treated (ATT).", footnote_as_chunk = T, general_title = " ") %>% footnote(general = "The standard errors in columns 2 and 4 are Abadie-Imbens SEs.", footnote_as_chunk = T, general_title = " ") %>% footnote(general = "*** = Significant at the p < .01 level, ** = Significant at the p < .05 level", footnote_as_chunk = T, general_title = " ")

c <- add_header_above(c, c(" " = 1, "All Observations " = 2, "Propensity Scores Between .1 and .9" = 2), bold = T, underline = F, font_size = 10)

c <- add_header_above(c, c("Table 7: Matching Estimates of the Sentencing Effect" = 5), bold = T, underline = F, font_size = 10) 

c <- column_spec(c, 1, width = "4.5cm", bold = T)

c <- column_spec(c, 2, width = "2.75cm", bold = T)

c <- column_spec(c, 3, width = "2.75cm", bold = T)

c <- column_spec(c, 4, width = "2.75cm", bold = T)

c <- column_spec(c, 5, width = "2.75cm", bold = T)

c

### Producing the Latex Code for Table A.2

sum.stats.u <- matrix(nrow = 4, ncol = 4)
sum.stats.u[1,] <- c("Gender", ".952", ".956", ".706")
sum.stats.u[2,] <- c("Race", ".345", ".408", ".017")
sum.stats.u[3,] <- c("# of Victims", "1.39", "1.40", ".647")
sum.stats.u[4,] <- c("# Jurisdiction", ".058", ".038", ".080")


sum.stats.u <- as.data.frame(sum.stats.u)
library(knitr)
library("kableExtra")
u <- kable(sum.stats.u, col.names = c("Variable", "Mean Treatment Value", "Mean Control Value", "P-value of Difference "), format = "latex", booktabs = T, align = "c") %>% kable_styling(font_size = 8, full_width = F)

u <- add_header_above(u, c("Table A.2: Post-Matching Balance Statistics (Propensity Scores .1-.9, with Replacement)" = 4), bold = T, underline = F, font_size = 10) 

u <- column_spec(u, 1, width = "4.5cm", bold = T)

u <- column_spec(u, 2, width = "2.75cm", bold = T)

u <- column_spec(u, 3, width = "2.75cm", bold = T)

u <- column_spec(u, 4, width = "2.75cm", bold = T)

u

### Producing the Latex Code for Table 10

## Few Cases for Each, so look at and tally by hand

Sykes.cases
Tymkovich.cases
Colloton.cases
Hardiman.cases
Willet.cases
Kethledge.cases
Barrett.cases
Gruender.cases
Stras.cases
Eid.cases
Gorsuch.cases
Grant.cases
Newsom.cases
Pryor.cases
Thapar.cases
Larsen.cases
Kavanaugh.cases


sum.stats.judges <- matrix(nrow = 12, ncol = 3)
sum.stats.judges[1,] <- c("Thomas Hardiman", "0", "2")
sum.stats.judges[2,] <- c("Raymond Kethledge", "0", "1")
sum.stats.judges[3,] <- c("Amul Thapar", "0", "0")
sum.stats.judges[4,] <- c("Amy Coney Barrett", "0", "0")
sum.stats.judges[5,] <- c("Diane Sykes", "1", "0")
sum.stats.judges[6,] <- c("Steven Colloton", "1", "0")
sum.stats.judges[7,] <- c("Raymond Gruender", "1", "0")
sum.stats.judges[8,] <- c("Neil Gorsuch", "0", "2")
sum.stats.judges[9,] <- c("Timothy Tymkovich", "2", "0")
sum.stats.judges[10,] <- c("Kevin Newsom", "0", "0")
sum.stats.judges[11,] <- c("William Pryor", "0", "0")
sum.stats.judges[12,] <- c("Brett Kavanaugh", "0", "0")

sum.stats.judges <- as.data.frame(sum.stats.judges)
library(knitr)
library("kableExtra")
q <- kable(sum.stats.judges, col.names = c("Judge", "Number of Votes for Capital Defendants", "Number of Votes for Non-Capital Defendants"), format = "latex", booktabs = T, align = "c") %>% kable_styling(font_size = 8, full_width = F)


q <- add_header_above(q, c("Table 10: Rulings of President Trump’s Supreme Court Shortlist in Capital Cases" = 3), bold = T, underline = F, font_size = 10) 

q <- column_spec(q, 1, width = "4cm", bold = T)

q <- column_spec(q, 2, width = "4cm", bold = T)

q <- column_spec(q, 3, width = "4cm", bold = T)

q <- row_spec(q, 5, color = "red")

q <- row_spec(q, 6, color = "red")

q <- row_spec(q, 7, color = "red")

q <- row_spec(q, 9, color = "red")

q



#############################################
##### Summary Statistics (Table 1) ##########
#############################################

capital.data <- capital.data[(capital.data$Conviction==1|capital.data$Conviction==.5|capital.data$Conviction==0) & (capital.data$D.s.Race==1|capital.data$D.s.Race==0),]

sum(ifelse((capital.data$Conviction==1|capital.data$Conviction==.5), 1, 0), na.rm = T)
sum(ifelse(capital.data$Conviction==0, 1, 0), na.rm = T)
sum(ifelse((capital.data$Conviction==1|capital.data$Conviction==.5) & capital.data$Sentence.at.Appeal==1, 1, 0), na.rm = T)
sum(ifelse(capital.data$Conviction==0 & capital.data$Sentence.at.Appeal==1, 1, 0), na.rm = T)
sum(ifelse((capital.data$Conviction==1|capital.data$Conviction==.5) & capital.data$Sentence.at.Appeal==0, 1, 0), na.rm = T)
sum(ifelse(capital.data$Conviction==0 & capital.data$Sentence.at.Appeal==0, 1, 0), na.rm = T)

sum(ifelse((capital.data$Sentence.at.Appeal==1|capital.data$Sentence.at.Appeal==.5), 1, 0), na.rm = T)
sum(ifelse(capital.data$Sentence.at.Appeal==0, 1, 0), na.rm = T)


sum(ifelse((capital.data$D.s.Gender==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$D.s.Gender==0, 1, 0), na.rm = T)
sum(ifelse((capital.data$D.s.Gender==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$D.s.Gender==0 & capital.data$Sentence.at.Appeal==1, 1, 0), na.rm = T)
sum(ifelse((capital.data$D.s.Gender==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)
sum(ifelse(capital.data$D.s.Gender==0 & capital.data$Sentence.at.Appeal==0, 1, 0), na.rm = T)

sum(ifelse((capital.data$Party==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$Party==0, 1, 0), na.rm = T)
sum(ifelse((capital.data$Party==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$Party==0 & capital.data$Sentence.at.Appeal==1, 1, 0), na.rm = T)
sum(ifelse((capital.data$Party==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)
sum(ifelse(capital.data$Party==0 & capital.data$Sentence.at.Appeal==0, 1, 0), na.rm = T)

sum(ifelse((capital.data$Type.of.Crime==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$Type.of.Crime==0, 1, 0), na.rm = T)
sum(ifelse((capital.data$Type.of.Crime==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$Type.of.Crime==0 & capital.data$Sentence.at.Appeal==1, 1, 0), na.rm = T)
sum(ifelse((capital.data$Type.of.Crime==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)
sum(ifelse(capital.data$Type.of.Crime==0 & capital.data$Sentence.at.Appeal==0, 1, 0), na.rm = T)

sum(ifelse((capital.data$Jurisidiction==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$Jurisidiction==0, 1, 0), na.rm = T)
sum(ifelse((capital.data$Jurisidiction==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$Jurisidiction==0 & capital.data$Sentence.at.Appeal==1, 1, 0), na.rm = T)
sum(ifelse((capital.data$Jurisidiction==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)
sum(ifelse(capital.data$Jurisidiction==0 & capital.data$Sentence.at.Appeal==0, 1, 0), na.rm = T)


mean(capital.data$Number.of.Victims, na.rm=T)
mean(capital.data[capital.data$Sentence.at.Appeal==1,]$Number.of.Victims, na.rm=T)
mean(capital.data[capital.data$Sentence.at.Appeal==0,]$Number.of.Victims, na.rm=T)

sum(ifelse((capital.data$D.s.Race==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$D.s.Race==0 & capital.data$Sentence.at.Appeal==1, 1, 0), na.rm = T)
sum(ifelse((capital.data$D.s.Race==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)
sum(ifelse(capital.data$D.s.Race==0 & capital.data$Sentence.at.Appeal==0, 1, 0), na.rm = T)



sum(ifelse((capital.data$Federal.Circuit.Court.Number.1==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.1==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.1==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)



sum(ifelse((capital.data$Federal.Circuit.Court.Number.3==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.3==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.3==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)

sum(ifelse((capital.data$Federal.Circuit.Court.Number.4==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.4==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.4==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)


sum(ifelse((capital.data$Federal.Circuit.Court.Number.5==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.5==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.5==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)


sum(ifelse((capital.data$Federal.Circuit.Court.Number.6==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.6==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.6==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)

sum(ifelse((capital.data$Federal.Circuit.Court.Number.7==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.7==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.7==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)

sum(ifelse((capital.data$Federal.Circuit.Court.Number.8==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.8==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.8==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)

sum(ifelse((capital.data$Federal.Circuit.Court.Number.9==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.9==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.9==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)

sum(ifelse((capital.data$Federal.Circuit.Court.Number.10==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.10==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.10==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)


sum(ifelse((capital.data$Federal.Circuit.Court.Number.11==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.11==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.11==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)

sum(ifelse((capital.data$Federal.Circuit.Court.Number.SCOTUS==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.SCOTUS==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$Federal.Circuit.Court.Number.SCOTUS==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)

sum(ifelse((capital.data$d.public==1), 1, 0), na.rm = T)
sum(ifelse((capital.data$d.public==0), 1, 0), na.rm = T)

sum(ifelse((capital.data$d.public==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$d.public==0 & capital.data$Sentence.at.Appeal==1, 1, 0), na.rm = T)

sum(ifelse((capital.data$d.public==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)
sum(ifelse(capital.data$d.public==0 & capital.data$Sentence.at.Appeal==0, 1, 0), na.rm = T)


mean(capital.data[capital.data$Sentence.at.Appeal==1,]$n.defendent.attorneys, na.rm = T)
mean(capital.data[capital.data$Sentence.at.Appeal==0,]$n.defendent.attorneys, na.rm = T)

sum(ifelse((capital.data$d.public==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$d.public==0 & capital.data$Sentence.at.Appeal==1, 1, 0), na.rm = T)

sum(ifelse((capital.data$en.banc==1 & capital.data$Sentence.at.Appeal==1), 1, 0), na.rm = T)
sum(ifelse(capital.data$en.banc==0 & capital.data$Sentence.at.Appeal==1, 1, 0), na.rm = T)

sum(ifelse((capital.data$en.banc==1 & capital.data$Sentence.at.Appeal==0), 1, 0), na.rm = T)
sum(ifelse(capital.data$en.banc==0 & capital.data$Sentence.at.Appeal==0, 1, 0), na.rm = T)

### Creating Latex Code for Table 1 ###

sum.stats <- matrix(nrow = 12, ncol = 4)
sum.stats[1,] <- c("Vote on Guilt-Phase Claim", "293 Favorable to D, 1075 Otherwise", "139 Favorable to D, Otherwise 490", "154 Favorable to D, Otherwise 585")
sum.stats[2,] <- c("Sentence at Time Case Decided", "629 Death, 739 Life-in-Prison", "NA", "NA")
sum.stats[3,] <- c("Defendant's Gender", "1309 Male, 59 Female", "620 Male, 9 Female", "689 Male, 50 Female")
sum.stats[4,] <- c("Party of Judge's Appointing President", "686 Dems, 682 Reps", "323 Dems, 306 Reps", "363 Dems, 376 Reps")
sum.stats[5,] <- c("Type of Case", "57 Federal, 1311 State", "18 Federal, 611 State", "39 Federal, 700 State")
sum.stats[6,] <- c("Type of Crime", "1216 First Degree Murder or Equivalent, 152 Otherwise", "626 First Degree Murder or Equivalent, 3 Otherwise", "590 First Degree Murder or Equivalent, 149 Otherwise")
sum.stats[7,] <- c("Average Number of Victims", "1.4", "1.55", "1.27")
sum.stats[8,] <- c("Defendant's Race", "540 White, 828 Otherwise", "294 White, 335 Otherwise", "246 White, 493 Otherwise")
sum.stats[9,] <- c("Court", "1st Cir. = 39, 2d Cir. = 6, 3d Cir. = 109, 4th Cir. = 45, 5th Cir. = 126, 6th Cir. = 282, 7th Cir. = 66, 8th = 36, 9th Cir. = 306, 10th Cir. = 102, 11th Cir. = 213, D.C. Cir. = 3,  SCOTUS = 35", "1st Cir. = 0, 2d Cir. = 0, 3d Cir. = 25, 4th Cir. = 24, 5th Cir. = 63, 6th Cir. = 81, 7th Cir. = 20, 8th = 15, 9th Cir. = 173, 10th Cir. = 72, 11th Cir. = 147, D.C. Cir. = 0, SCOTUS = 9", "1st Cir. = 39, 2d Cir. = 6, 3d Cir. = 84, 4th Cir. = 21, 5th Cir. = 63, 6th Cir. = 201, 7th Cir. = 46, 8th = 21, 9th Cir. = 133, 10th Cir. = 30, 11th Cir. = 66, D.C. Cir. = 3, SCOTUS = 26")
sum.stats[10,] <- c("Public Defense Team", "368 Public, 874 Private", "222 Public, 404 Private", "146 Public, 470 Private")
sum.stats[11,] <- c("Average Number of Defense Attorneys", "1.96", "2.38", "1.55")
sum.stats[12,] <- c("En Banc", "129 En Banc, 1239 Otherwise", "74 En Banc, 555 Otherwise", "55 En Banc, 684 Otherwise")

sum.stats <- as.data.frame(sum.stats)
library(knitr)
library("kableExtra")
z <- kable(sum.stats, col.names = c("Variable", "Full Sample", "Capital Defendants Only", "Non-Capital Defendants Only"), format = "latex", align = "c", booktabs = TRUE) %>% kable_styling(latex_options = c("scale_down", "basic"), font_size = 8, full_width = F) %>% row_spec(0, bold = T) %>% footnote(general = "N = 1377 (Except 'Public Defense Team' as some defendants had no attorney)", footnote_as_chunk = T, general_title = " ") %>% row_spec(1, hline_after = T) %>% row_spec(2, hline_after = T) %>% row_spec(3, hline_after = T) %>% row_spec(4, hline_after = T) %>% row_spec(5, hline_after = T) %>% row_spec(6, hline_after = T) %>% row_spec(7, hline_after = T) %>% row_spec(8, hline_after = T) %>% row_spec(9, hline_after = T) %>% row_spec(10, hline_after = T) %>% row_spec(11, hline_after = T)

z <- add_header_above(z, c("Table 1: Summary Statistics" = 4), bold = T, underline = F, font_size = 10) 

z <- column_spec(z, 1, width = "5cm", bold = T)

z <- column_spec(z, 2, width = "3cm")

z <- column_spec(z, 3, width = "3cm")

z <- column_spec(z, 4, width = "3cm")

z




## Testing Claim from Foonote (105)

capital.data$D.s.Race.original <- capital.data$D.s.Race

CSE.regression.fine.grained.test <- felm(Conviction ~  Sentence.at.Appeal +  D.s.Gender +  Party + Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year + as.factor(race.original)|Federal.Circuit.Court.Number + State.of.Imprisonment|0|Case.Name, data = capital.data)
summary(CSE.regression.fine.grained.test)

CSE.regression.judge.fixed.fine.grained <- felm(Conviction ~ Sentence.at.Appeal +  D.s.Gender + Type.of.Crime +  Number.of.Victims +  Jurisidiction + Year + as.factor(race.original)|Judge + State.of.Imprisonment|0|Case.Name, data = capital.data)
summary(CSE.regression.judge.fixed.fine.grained)

covariate.names.nv <- c("Sentence at Time of Appeal (1 if Death, 0 Otherwise)", "Male (1 if Male, 0 Otherwise)", "Party of Appointing President (1 if Dem, 0 Otherwise)", "Type of Crime (1 if 1st Degree Murder or Equivalent, 0 Otherwise)", "Number of Victims", "Jurisdiction (1 if Federal, 0 Otherwise)", "Year")
dv.name.nv <- c("Guilt-Phase Vote (1 if Favorable to Defendant, 0 Otherwise)")

require("stargazer")
stargazer(CSE.regression.fine.grained.test, CSE.regression.judge.fixed.fine.grained, title="Table A.6: More Specific Racial Coding", align=T, type = "latex", report = ('vc*s'), multicolumn=T, covariate.labels = covariate.names.nv, omit=c("Federal.Circuit.Court.Number", "Judge", "State.of.Imprisonment", "Constant", "race.original"), dep.var.labels = dv.name.nv, column.labels = c("CSE", "CSE"), column.separate = c(1,1), add.lines = list(c("Court-Level Fixed Effects", "Yes","No","Yes","No", "Yes","No"), c("Judge-Level Fixed Effects", "No","Yes"), c("State-Level Fixed Effects", "Yes","Yes"), c("Observations", "1377","1377"), c("Multiple Racial Categories", "Yes","Yes")), omit.stat = c("adj.rsq", "f", "ser", "rsq", "n"), model.names = F, notes = "Interpretations: *p < .1; **p < .05; ***p < .01 ", notes.append = F, notes.align = "l", column.sep.width = "-15pt")


